package plgdb

import (
	"dgo/goutils/utils"
	"time"
)

type KvItem struct {
	Id     string
	Val    interface{}
	Uptime time.Time
	Tag    interface{}
}

func CheckGetKvItem(lst *utils.GSyncMapGroup, id string) *KvItem {
	return CheckGetKvItemEx(lst, id, nil)
}

func CheckGetKvItemEx(lst *utils.GSyncMapGroup, id string, newtagfunc func() interface{}) *KvItem {
	return lst.LoadOrStoreFunc(id, func() interface{} {
		if newtagfunc == nil {
			return &KvItem{Id: id}
		} else {
			return &KvItem{Id: id, Tag: newtagfunc()}
		}
	}, nil).(*KvItem)
}

func FindKvItem(lst *utils.GSyncMapGroup, id string) *KvItem {
	obj := lst.Get(id, nil)
	if obj == nil {
		return nil
	}
	return obj.(*KvItem)
}
